home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / trans / transpar.frm < prev    next >
Text File  |  1996-10-29  |  6KB  |  144 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   4800
  5.    ClientLeft      =   3885
  6.    ClientTop       =   2085
  7.    ClientWidth     =   2730
  8.    Height          =   5205
  9.    Left            =   3825
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   320
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   182
  14.    Top             =   1740
  15.    Width           =   2850
  16.    Begin VB.CommandButton Command1 
  17.       Caption         =   "TransparentBlt"
  18.       Height          =   375
  19.       Left            =   360
  20.       TabIndex        =   1
  21.       Top             =   1920
  22.       Width           =   1935
  23.    End
  24.    Begin VB.PictureBox Picture1 
  25.       AutoSize        =   -1  'True
  26.       Height          =   960
  27.       Left            =   360
  28.       Picture         =   "Transparent sample.frx":0000
  29.       ScaleHeight     =   60
  30.       ScaleMode       =   3  'Pixel
  31.       ScaleWidth      =   120
  32.       TabIndex        =   0
  33.       Top             =   2400
  34.       Width           =   1860
  35.    End
  36. End
  37. Attribute VB_Name = "Form1"
  38. Attribute VB_Creatable = False
  39. Attribute VB_Exposed = False
  40. Option Explicit
  41.  
  42. Private Type RECT
  43.   Left As Long
  44.   Top As Long
  45.   Right As Long
  46.   Bottom As Long
  47. End Type
  48.  
  49. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  50. Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
  51. Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
  52. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  53. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  54. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  55. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  56. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  57.  
  58. Private Sub Command1_Click()
  59.   Dim R As RECT
  60.   With R
  61.     R.Left = 20
  62.     R.Top = 20
  63.     R.Right = Picture1.ScaleWidth - 20
  64.     R.Bottom = Picture1.ScaleHeight - 20
  65.   End With
  66.   TransparentBlt Form1.hdc, Form1.hdc, Picture1.hdc, R, 40, 40, vbWhite
  67. End Sub
  68.  
  69.  
  70. Private Sub TransparentBlt(OutDstDC, DstDC, SrcDC, SrcRect As RECT, DstX, DstY, TransColor As Long)
  71.   'DstDC=Device context into which image must be drawn transparently
  72.   'OutDstDC=Device context into image is actually drawn, even though it is made transparent in terms of DstDC
  73.   'Src=Device context of source to be made transparent in color TransColor
  74.   'SrcRect=rectangular region within SrcDC to be made transparent in terms of DstDC, and drawn to OutDstDC
  75.   'DstX, DstY =coordinates in OutDstDC (and DstDC) where tranparent bitmap must go
  76.   
  77.   Rem In most cases, OutDstDC and DstDC will be the same
  78.   
  79.   Dim nRet As Long, W As Integer, H As Integer
  80.   Dim MonoMaskDC As Long, hMonoMask As Long
  81.   Dim MonoInvDC As Long, hMonoInv As Long
  82.   Dim ResultDstDC As Long, hResultDst As Long
  83.   Dim ResultSrcDC As Long, hResultSrc As Long
  84.   Dim hPrevMask As Long, hPrevInv As Long, hPrevSrc As Long, hPrevDst As Long
  85.   W = SrcRect.Right - SrcRect.Left + 1
  86.   H = SrcRect.Bottom - SrcRect.Top + 1
  87.   
  88.   'create monochrome mask and inverse masks
  89.   MonoMaskDC = CreateCompatibleDC(DstDC)
  90.   MonoInvDC = CreateCompatibleDC(DstDC)
  91.   hMonoMask = CreateBitmap(W, H, 1, 1, ByVal 0&)
  92.   hMonoInv = CreateBitmap(W, H, 1, 1, ByVal 0&)
  93.   hPrevMask = SelectObject(MonoMaskDC, hMonoMask)
  94.   hPrevInv = SelectObject(MonoInvDC, hMonoInv)
  95.   
  96.   'create keeper DCs and bitmaps
  97.   ResultDstDC = CreateCompatibleDC(DstDC)
  98.   ResultSrcDC = CreateCompatibleDC(DstDC)
  99.   hResultDst = CreateCompatibleBitmap(DstDC, W, H)
  100.   hResultSrc = CreateCompatibleBitmap(DstDC, W, H)
  101.   hPrevDst = SelectObject(ResultDstDC, hResultDst)
  102.   hPrevSrc = SelectObject(ResultSrcDC, hResultSrc)
  103.   
  104.   'copy src to monochrome mask
  105.   Dim OldBC As Long
  106.   OldBC = SetBkColor(SrcDC, TransColor)
  107.   nRet = BitBlt(MonoMaskDC, 0, 0, W, H, SrcDC, SrcRect.Left, SrcRect.Top, vbSrcCopy)
  108.   TransColor = SetBkColor(SrcDC, OldBC)
  109.   
  110.   'create inverse of mask
  111.   nRet = BitBlt(MonoInvDC, 0, 0, W, H, MonoMaskDC, 0, 0, vbNotSrcCopy)
  112.   
  113.   'get background
  114.   nRet = BitBlt(ResultDstDC, 0, 0, W, H, DstDC, DstX, DstY, vbSrcCopy)
  115.   'AND with Monochrome mask
  116.   nRet = BitBlt(ResultDstDC, 0, 0, W, H, MonoMaskDC, 0, 0, vbSrcAnd)
  117.   'get overlapper
  118.   nRet = BitBlt(ResultSrcDC, 0, 0, W, H, SrcDC, SrcRect.Left, SrcRect.Top, vbSrcCopy)
  119.   'AND with inverse monochrome mask
  120.   nRet = BitBlt(ResultSrcDC, 0, 0, W, H, MonoInvDC, 0, 0, vbSrcAnd)
  121.   'XOR these two
  122.   nRet = BitBlt(ResultDstDC, 0, 0, W, H, ResultSrcDC, 0, 0, vbSrcInvert)
  123.   
  124.   'output results
  125.   nRet = BitBlt(OutDstDC, DstX, DstY, W, H, ResultDstDC, 0, 0, vbSrcCopy)
  126.   
  127.   'clean up
  128.   hMonoMask = SelectObject(MonoMaskDC, hPrevMask)
  129.   DeleteObject hMonoMask
  130.   hMonoInv = SelectObject(MonoInvDC, hPrevInv)
  131.   DeleteObject hMonoInv
  132.   hResultDst = SelectObject(ResultDstDC, hPrevDst)
  133.   DeleteObject hResultDst
  134.   hResultSrc = SelectObject(ResultSrcDC, hPrevSrc)
  135.   DeleteObject hResultSrc
  136.   DeleteDC MonoMaskDC
  137.   DeleteDC MonoInvDC
  138.   DeleteDC ResultDstDC
  139.   DeleteDC ResultSrcDC
  140.   
  141.   
  142. End Sub
  143.  
  144.